package model

import business.Attackable
import business.Blockable
import business.Moveable
import business.Sufferable
import org.itheima.kotlin.game.core.Painter

/**
 * created by lucus on 2019/4/4
 * e-mail: mcphilex1989@gmail.com
 * phone: 15652608401
 */
class Tank(override var x: Int, override var y: Int) : Moveable, Sufferable {

    // 我方坦克血量
    override var blood: Int = 20

    override val speed: Int = 8

    override val width: Int = BaseConfig.GAME_UNIT
    override val height: Int = BaseConfig.GAME_UNIT

    override var mCurrentDirection: Direction = Direction.UP

    private var badDirection: Direction? = null

    override fun draw() {

        val tankPath = when (mCurrentDirection) {
            Direction.UP -> "img/tank_u.gif"
            Direction.DOWN -> "img/tank_d.gif"
            Direction.LEFT -> "img/tank_l.gif"
            Direction.RIGHT -> "img/tank_r.gif"
        }

        Painter.drawImage(tankPath, x, y)
    }

    fun move(direction: Direction) {

        // 判断是否是碰撞的direction,如果是，不移动
        if (direction == this.badDirection) {
            return
        }

        if (this.mCurrentDirection != direction) {
            this.mCurrentDirection = direction
            return
        }
        when (direction) {
            Direction.UP -> y -= speed
            Direction.DOWN -> y += speed
            Direction.LEFT -> x -= speed
            Direction.RIGHT -> x += speed
        }

        if (x < 0) x = 0
        if (x > BaseConfig.GAME_WINDOW_WIDTH - width) x = BaseConfig.GAME_WINDOW_WIDTH - width
        if (y < 0) y = 0
        if (y > BaseConfig.GAME_WINDOW_HEIGHT - height) y = BaseConfig.GAME_WINDOW_HEIGHT - height
    }

    override fun notifyCollision(direction: Direction?, blockable: Blockable?) {
        this.badDirection = direction
    }

    /**
     * 发射子弹的方法
     */
    fun shot(): Bullet {
        return Bullet(this,mCurrentDirection) { bulletWidth, bulletHeight ->

            val tankX = x
            val tankY = y
            val tankWidth = width
            val tankHeight = height

            var bulletX: Int = 0
            var bulletY: Int = 0

            // 计算 子弹的坐标
            when (mCurrentDirection) {
                Direction.UP -> {
                    bulletX = tankX + (tankWidth - bulletWidth) / 2
                    bulletY = tankY - bulletHeight / 2
                }

                Direction.DOWN -> {
                    bulletX = tankX + (tankWidth - bulletWidth) / 2
                    bulletY = tankY + tankHeight - bulletHeight / 2
                }

                Direction.RIGHT -> {
                    bulletX = tankX + tankWidth - bulletWidth / 2
                    bulletY = tankY + (tankHeight - bulletHeight) / 2
                }

                Direction.LEFT -> {
                    bulletX = tankX - bulletWidth / 2
                    bulletY = tankY + (tankHeight - bulletHeight) / 2
                }

            }
            Pair(bulletX, bulletY)
        }
    }

    override fun notifySuffer(attackable: Attackable): Array<IView>? {
        blood -= attackable.attackBlood
        return arrayOf(Blast(x, y))
    }


}